package com.zhida.jiyangcenter.widget

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout
import android.widget.TextView
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.widget.Utils.dp2px
import java.util.*
import kotlin.math.cos
import kotlin.math.sin


class CircleMenuArchives @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    interface OnWaterItemListener {
        fun onItemClick(waterModel: WaterModel?)
    }

    private var mOnWaterItemListener: OnWaterItemListener? = null

    /**
     * 坐标X
     */
    private var treeCenterX = 0f

    /**
     * 坐标Y
     */
    private var treeCenterY = 0f

    private var mMenuItemRadius // ItemView 到圆心的半径
            = 0f

    // 每个ItemView之间的角度差
    private var mAngle = 0.0

    init {
        // 获取半径
        mMenuItemRadius = dp2px(context, 100f).toFloat()

    }
    private val mOffsets = Arrays.asList(5.0f, 4.5f, 4.8f, 5.5f, 5.8f, 6.0f, 6.5f)

    override fun performClick(): Boolean {
        return super.performClick()
    }
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
    }


    /**
     * 设置小球数据，根据数据集合创建小球数量
     *
     * @param modelList 数据集合
     */
    fun setModelList(
        modelList: List<WaterModel?>?,
        view: View
    ) {
        if (modelList == null || modelList.isEmpty()) {
            return
        }
        treeCenterX = view.x+dp2px(context, 38f).toFloat()
        treeCenterY = view.y+dp2px(context, 38f).toFloat()
        removeAllViews()
        post { addWaterView(modelList) }
    }

    private fun addWaterView(modelList: List<WaterModel?>) {
        mAngle = Math.PI / (modelList.size - 1)
        modelList.forEachIndexed { index, waterModel ->
            val child = LayoutInflater.from(context).inflate(R.layout.item_circle_menu,this,false) as TextView
            child.text = waterModel?.content ?: ""

            // 动态的计算角度
            val curAngle: Double = Math.PI - mAngle * index
            val childCenterX =
                (treeCenterX + mMenuItemRadius * cos(curAngle)).toInt()
            val childCenterY =
                (treeCenterY - mMenuItemRadius * sin(curAngle)).toInt()
            child.x= (childCenterX - child.measuredWidth/2).toFloat()
            child.y= (childCenterY - child.measuredWidth/2).toFloat()

            child.tag = waterModel

            child.setTag(R.string.isUp,index==modelList.size)
            setOffset(child, index)
            addView(child)
//            addShowViewAnimation(child)
//            start(child)
        }
    }

    fun start(view: View) {
        val isUp = view.getTag(R.string.isUp) as Boolean
        val offset = view.getTag(R.string.offset) as Float
        var mAnimator: ObjectAnimator? = null
        mAnimator = if (isUp) {
            ObjectAnimator.ofFloat(
                view,
                "translationY",
                view.y - offset,
                view.y + offset,
                view.y - offset
            )
        } else {
            ObjectAnimator.ofFloat(
                view,
                "translationY",
                view.y + offset,
                view.y - offset,
                view.y + offset
            )
        }
        mAnimator.duration = 1800
        mAnimator.interpolator = LinearInterpolator()
        mAnimator.repeatMode = ValueAnimator.RESTART
        mAnimator.repeatCount = ValueAnimator.INFINITE
        mAnimator.start()
    }


    /**
     * 添加显示动画
     *
     * @param view
     */
    private fun addShowViewAnimation(view: View) {
        view.alpha = 0f
        view.scaleX = 0f
        view.scaleY = 0f
        view.animate().alpha(1f).scaleX(1f).scaleY(1f).setDuration(500).start()
    }
    /**
     * 设置小球点击事件
     *
     * @param onWaterItemListener
     */
    fun setOnWaterItemListener(onWaterItemListener: OnWaterItemListener) {
        mOnWaterItemListener = onWaterItemListener
    }
    /**
     * 设置View的offset
     *
     * @param view
     */
    private fun setOffset(view: View, index: Int) {
        val offset = mOffsets[index]
        view.setTag(R.string.offset, offset)
    }
}